package org.bankabeule.ui.views;


import org.bankabeule.Model;
import org.bankabeule.core.ModelManager;
import org.bankabeule.util.Providers;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.forms.widgets.FormToolkit;
import org.eclipse.ui.forms.widgets.Section;


public class ModelView extends AbstractViewPart {
	

	/**
	 * ID
	 */
	public static final String ID = "org.bankabeule.ui.views.ModelView";
	

	/**
	 * Section
	 */
	private Section section;
	
	
	/**
	 * Tree viewer
	 */
	private TreeViewer viewer;
	
	
	/**
	 * The constructor.
	 */
	public ModelView() {
		super();
	}

	
	/**
	 * This is a callback that will allow us
	 * to create the viewer and initialize it.
	 */
	public void createPartControl(Composite parent) {				
		FormToolkit tk = new FormToolkit(parent.getDisplay());
		
		section = tk.createSection(parent, Section.TITLE_BAR);
		section.setText("Accounts");
		Composite sectionComposite = tk.createComposite(section);
		section.setClient(sectionComposite);
		sectionComposite.setLayout(new FillLayout());

		viewer = new TreeViewer(sectionComposite);
		viewer.setContentProvider(Providers.getContentProvider());
		viewer.setLabelProvider(Providers.getLabelProvider());
		
		ModelManager.get().addModelListener(this);
	}
	

	/**
	 * Passing the focus request to the viewer's control.
	 */
	public void setFocus() {
		viewer.getTree().setFocus();
	}


	public void modelLoaded(Model model) {
		viewer.setInput(model);
		section.setEnabled(true);
	}

	public void modelUnloaded(Model model) {
		viewer.setInput(null);
		section.setEnabled(false);
	}

	
}